﻿using CarAppDomain.CarParts.Entities;
using CarAppDomain.CarType.Entities;
using CarAppDomain.CarType.IRepository;
using CarAppDto.CarParts.CarPartsDtos;
using CarAppDto.CarType.VehicleSeriesDtos;
using D.UtilCore.Dtos;
using D.UtilSqlsugarOrm;
using SqlSugar;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CarAppDomain.CarType.Repository
{
    public class VehicleSeriesEntityRepository : BaseRepository<VehicleSeriesEntity>, IVehicleSeriesEntityRepository
    {
        private readonly ISqlSugarClient sqlSugarClient;
        public VehicleSeriesEntityRepository(ISqlSugarClient db) : base(db)
        {
            sqlSugarClient = db;
        }

        /// <summary>
        /// 获取所有列表
        /// </summary>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<List<RepVehicleSeries>> GetListAsync(System.Linq.Expressions.Expression<Func<VehicleSeriesEntity, bool>> where = null, System.Linq.Expressions.Expression<Func<VehicleSeriesEntity, object>> order = null)
        {
         
                return await sqlSugarClient.Queryable<VehicleSeriesEntity>()
               .LeftJoin<AreaEntity>((m, d) => m.SaleAreaId == d.Id)
              .LeftJoin<CountryEntity>((m,d, c) => m.ManufacturingCountryId == c.Id)
              .LeftJoin<BrandEntity>((m, d, c,f) => m.BrandId == f.Id)
              .WhereIF(where != null, where)
              .OrderByIF(order != null, order).Select<RepVehicleSeries>().ToListAsync();        
        }       

        /// <summary>
        /// 分页查询 加入条件 和 排序
        /// </summary>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="current">当前第几页</param>
        /// <param name="where"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        public async Task<PageModel<RepVehicleSeries>> GetOrderPagesAsync(int pageSize, int current, System.Linq.Expressions.Expression<Func<VehicleSeriesEntity, bool>> where, System.Linq.Expressions.Expression<Func<VehicleSeriesEntity, object>> order = null, bool OrderByAsc = true)
        {           
                RefAsync<int> totalCount = 0;//REF和OUT不支持异步,想要真的异步这是最优解
                OrderByType type = OrderByType.Asc;
                if (!OrderByAsc)
                    type = OrderByType.Desc;
                var list = await sqlSugarClient.Queryable<VehicleSeriesEntity>().LeftJoin<AreaEntity>((m, d) => m.SaleAreaId == d.Id)
                             .LeftJoin<CountryEntity>((m,d, c) => m.ManufacturingCountryId == c.Id)
                              .LeftJoin<BrandEntity>((m, d, c, f) => m.BrandId == f.Id).WhereIF(where != null, where)
                             .OrderByIF(order != null, order, type).Select<RepVehicleSeries>().ToPageListAsync(current, pageSize, totalCount);
                return new PageModel<RepVehicleSeries>() { Data = list, TotalCount = totalCount, TotalPage = (int)Math.Ceiling(totalCount / (double)pageSize) };
         }

        /// <summary>
        /// 添加配件
        /// </summary>
        /// <param name="model"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public async Task<int> AddCarPartsAsync(List<CarPartsVehicleSeriesEntity> modelList)
        {
          
           return  await sqlSugarClient.Insertable(modelList).ExecuteCommandAsync();

        }

        public async Task<int> DeleteCarPartsAsync(List<string> ids)
        {
            return await sqlSugarClient.Deleteable<CarPartsVehicleSeriesEntity>().Where(it => ids.Contains(it.Id)).ExecuteCommandAsync();          
        }
    }
}
